package karileet.meeting.profile.model;

import java.util.HashMap;

/**
 * Model object for complex data exchange between user preferences and app
 * Created by markus on 31.10.14.
 */
public class Weekdays {
    //static int values are position values in the list
    public static int MON = 0;
    public static int TUE = 1;
    public static int WED = 2;
    public static int THU = 3;
    public static int FRI = 4;
    public static int SA = 5;
    public static int SU = 6;

    private HashMap<Integer, Boolean> days = new HashMap<Integer, Boolean>();

    public Weekdays() {
        days.put(MON, false);
        days.put(TUE, false);
        days.put(WED, false);
        days.put(THU, false);
        days.put(FRI, false);
        days.put(SA, false);
        days.put(SU, false);
    }

    public boolean isDayEnabled(Integer day) {
        if (days.containsKey(day)) {
            return days.get(day);
        } else {
            return false;
        }

    }

    public void setDayEnabled(Integer day) {
        days.put(day, true);
    }

    public void set(Integer day, Boolean state) {
        days.put(day, state);
    }

    public void stringToWeekdays(String string) {
        if (isOldFormat(string)) {
            string = oldFormatParser(string);
        }
        String[] days = string.split("");
        for (String day : days) {
            if (!day.equals("")) {
                setDayEnabled(Integer.parseInt(day));
            }
        }
    }

    /**
     * Check whether string is old weekday format with semicolons
     *
     * @param string String to check
     * @return
     */
    private boolean isOldFormat(String string) {
        return string.indexOf(";") > 0;
    }

    /**
     * Replace old format of string with semicolons with new format
     *
     * @return String without semicolons
     */
    private String oldFormatParser(String oldformat) {
        return oldformat.replaceAll(";", "");
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (java.util.Map.Entry<Integer, Boolean> set : days.entrySet()) {
            if (set.getValue()) {
                sb.append(set.getKey());
            }
        }
        return sb.toString();
    }
}
